<template>
  <div class='container'>
    <el-card>
     <!-- 面包屑 -->
    <el-breadcrumb v-if="show" separator-class="el-icon-arrow-right">
      <el-breadcrumb-item >学科管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{rowdata.subjectName}}</el-breadcrumb-item>
      <el-breadcrumb-item>目录管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row>
      <el-col :span="22">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="目录名称">
              <el-input v-model.trim="formInline.directoryName" placeholder="请输入目录名称"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="formInline.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-button  size="small" @click="clefn">清除</el-button>
            <el-button type="primary" size="small" @click="seekfn" >搜索</el-button>
            <el-link type="primary" v-if="show" icon="el-icon-back" @click="$router.push({ path: 'list' })">返回学科</el-link>
        </el-form>

      </el-col>
      <el-col :span="2">
        <el-button type="success" class="btn" size="small" icon="el-icon-edit" @click="addfn()">新增目录</el-button>
      </el-col>
    </el-row>
      <el-alert
      :title="`数据一共 ${counts} 条`"
      type="info"
      show-icon
      :closable="false">
  </el-alert>
   <!-- 头部 -->

   <!-- 主体 -->
    <template>
    <el-table
      :data="tableData"
      :header-cell-style="{background:'#fafafa','border-bottom':'3px solid #e8e8e8'}"
      style="width: 100%">
      <el-table-column
        label="序号"
        align="center"
        type="index">
      </el-table-column>
      <el-table-column
      align="center"
        prop="subjectName"
        label="所属学科">
      </el-table-column>
      <el-table-column
      align="center"
        prop="directoryName"
        label="目录名称">
      </el-table-column>
      <el-table-column
      align="center"
        prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
      align="center"
        label="创建日期">
        <template slot-scope="scope">
          {{scope.row.addDate|addDate}}
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        prop="totals"
        label="面试题数量">
      </el-table-column>
      <el-table-column
      align="center"
        label="状态">
        <template slot-scope="scope">
          {{scope.row.state===1?'已启用':'已禁用'}}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="stopfn(scope.row)" >
            {{scope.row.state==0?'启用':'禁用'}}
          </el-button>
          <el-button  :disabled="scope.row.state===1" type="text"  @click="updatafn(scope.row)">修改</el-button>
          <el-button  :disabled="scope.row.state===1 || scope.row.totals!==0 " type="text" @click="delcilck(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </template>
  <!-- 分页 -->
  <el-pagination
      v-if="!show"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page=obj.page
      background
      :page-sizes="[5, 10, 20, 50]"
      :page-size=obj.pagesize
      layout="prev, pager, next, sizes,jumper"
      :total="counts">
    </el-pagination>
   <!-- 主体 -->
  </el-card>
  <!-- 弹框 -->
  <el-dialog
    :title="isedit?'修改目录':'新增目录'"
    :visible.sync="dialogVisible"
    width="20%">
    <directorysAdd v-if="dialogVisible" :dialogVisible.sync="dialogVisible" :isedit="isedit" :row="row" @success="success"></directorysAdd>
  </el-dialog>
  <!-- 弹框 -->
  </div>
</template>

<script>
import directorysAdd from '../components/directorys-add.vue'
import { list, remove, changeState } from '@/api/hmmm/directorys'
export default {
  components: {
    directorysAdd
  },
  data () {
    return {
      tableData: [],
      obj: {
        page: 1,
        pagesize: 10,
        subjectName: '',
        state: null
      },
      newobj: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      counts: 0,
      formInline: {
        directoryName: '',
        state: null,
        subjectName: ''
      },
      dialogVisible: false,
      isedit: false,
      row: {},
      show: false,
      rowdata: {}
    }
  },
  created () {
    this.getpath()
  },
  watch: {
    $route () {
      // console.log(966)
      this.show = false
      this.getupdate()
    }
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.obj.pagesize = val
      this.getupdate()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.obj.page = val
      this.getupdate()
    },
    // 用来接受传过来的row
    getpath () {
      const rowdata = this.$route.params.row
      // console.log(rowdata, 598)
      if (rowdata !== undefined) {
        this.show = true
        this.rowdata = rowdata
        this.getupdate()
      } else {
        this.show = false
        this.getupdate()
      }
    },
    // 获取数据
    async getupdate () {
      const res0 = await list(this.obj)
      // console.log(res.data, 89898)
      if (this.show) {
        const res = await list({ page: 1, pagesize: res0.data.counts })
        const arr = res.data.items
        const narr = []
        const newarr = arr.filter(item => {
          if (item.subjectName === this.rowdata.subjectName) {
            return narr.push(item)
          }
        })
        this.tableData = newarr
        this.counts = newarr.length
      } else {
        this.tableData = res0.data.items
        this.counts = res0.data.counts
      }
    },
    // 点击清除按钮
    clefn () {
      this.formInline = {
        directoryName: '',
        state: null
      }
      this.obj = this.formInline
      this.getupdate()
    },
    // 点击搜索按钮
    seekfn () {
      console.log(this.formInline, 565)
      // this.obj.state = this.formInline.state
      // this.obj.directoryName = this.formInline.directoryName
      this.obj = this.formInline
      this.getupdate()
    },
    // 点击操作中的删除
    delcilck (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row, { row })
        if (this.tableData.length === 1) {
          this.obj.page--
          if (this.obj.page === 1) {
            this.obj.page = 1
          }
        }
        this.getupdate()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击操作中的禁用
    async stopfn (row) {
      if (row.state === 0) {
        row.state = 1
      } else {
        row.state = 0
      }
      await changeState(row)
    },
    // 点击新增按钮
    addfn () {
      this.isedit = false
      this.dialogVisible = true
    },
    // 点击操作中的修改
    updatafn (row) {
      this.isedit = true
      this.row = row
      this.dialogVisible = true
    },
    // 子向父传值
    success () {
      this.dialogVisible = false
      this.getupdate()
    }

  }
}
</script>

<style scoped lang='scss'>
.container{
 margin: 10px 15px;
 .el-breadcrumb{
   margin-bottom: 20px;
 }
 .el-input{
  width: 200px;
  }
  .btn{
    margin: 0px 20px;
  }
  .sp{
    margin-right: 10px;
  }
  .el-alert{
    margin-top: 10px;
    margin-bottom: 20px;
  }
  .el-pagination{
    text-align: right;
    margin-top: 20px;
  }
  .el-link{
  float: right;
  margin-top: 10px;
  }
}

</style>
